﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using nRoute.Samples.SiteMapWeb.Model;
using System.Data.Objects;
using System.Data.Entity.ModelConfiguration;

namespace nRoute.Samples.SiteMapWeb.Data
{
	public class ApplicationSiteMapContext : DbContext
	{
		public DbSet<ApplicationSiteMap> SiteMaps { get; set; }

		public DbSet<ApplicationSiteMapArea> SiteMapAreas { get; set; }
		
		public DbSet<ApplicationSiteMapNode> SiteMapNodes { get; set; }

		internal ObjectContext InnerContext
		{
			// see http://csharpbits.notaclue.net/2010/07/using-entity-framework-code-first-ctp4.html
			get { return this.InnerContext; }
		}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

			modelBuilder.Entity<ApplicationSiteMap>().HasMany((a) => a.SiteMapAreas).WithOptional();
			modelBuilder.Entity<ApplicationSiteMapArea>().HasMany((a) => a.AreaDependencies).WithOptional();
			modelBuilder.Entity<ApplicationSiteMapNode>().HasMany((a) => a.ChildNodes).WithOptional();			
		}
	}
}